<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="container">
      <button class="enterFillScreen">进入全屏</button>
      <button class="exitFillScreen">退出全屏</button>
    </div>

    <script>
      // https://wohugb.gitbooks.io/javascript/content/htmlapi/fullscreen.html
      // https://coder.itclan.net/fontend/js/enter-the-full-screen/#进入全屏模式

      // 进入全屏函数
      function enterFullscreen(element) {
        if (element.requestFullscreen) {
          element.requestFullscreen()
        } else if (element.mozRequestFullScreen) {
          element.mozRequestFullScreen()
        } else if (element.msRequestFullscreen) {
          element.msRequestFullscreen()
        } else if (element.webkitRequestFullscreen) {
          element.webkitRequestFullScreen()
        }
      }
      // 退出全屏函数
      function exitFullscreen() {
        if (document.exitFullscreen) {
          document.exitFullscreen()
        } else if (document.msExitFullscreen) {
          document.msExitFullscreen()
        } else if (document.mozCancelFullScreen) {
          document.mozCancelFullScreen()
        } else if (document.webkitExitFullscreen) {
          document.webkitExitFullscreen()
        }
      }

      const oEnterButton = document.querySelector('.enterFillScreen')
      const oExitButtton = document.querySelector('.exitFillScreen')
      oEnterButton.addEventListener('click', () => {
        enterFullscreen(document.documentElement)
      })
      oExitButtton.addEventListener('click', () => {
        exitFullscreen(document.documentElement)
      })

      // fullscreenchange事件：浏览器进入或离开全屏时触发。
      // fullscreenerror事件：浏览器无法进入全屏时触发，可能是技术原因，也可能是用户拒绝。
      document.addEventListener('fullscreenchange', function (event) {
        if (document.fullscreenElement) {
          console.log('进入全屏')
        } else {
          console.log('退出全屏')
        }
      })
    </script>
  </body>
</html>
